package com.jiyun.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.Visitor;
import com.jiyun.service.IVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-04-18
 */
@RestController
@RequestMapping("/visitor")
public class VisitorController {

    @Autowired
    IVisitorService visitorService;


    @RequestMapping("findAllNum")
    public Integer findAllNum(){
        return visitorService.count();
    }

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addVisitor(@RequestBody Visitor visitor){
        visitorService.save(visitor);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateVisitor(@RequestBody Visitor visitor){
        visitorService.updateById(visitor);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        visitorService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        visitorService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Visitor findById(@RequestParam Integer id){
        return visitorService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Visitor> findAll(){
        return visitorService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<Visitor> findPage(@RequestParam Integer page,@RequestParam Integer pageSize){
        Page<Visitor> visitorPage = new Page<>(page, pageSize);
        return visitorService.page(visitorPage);
    }

}
